package com.sanalkitaplik.usermanagement;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import com.sanalkitaplik.hibernateutil.BooksUtil;
import com.sanalkitaplik.hibernateutil.StaticInfo;
import com.sanalkitaplik.resources.Book;

public class UserBookManager {
	
	private List<Book> books;
	private boolean addNewBookActive;
	private boolean editBookActive;
	private Book editingBook;
	private Book addingBook;
	private List<SelectItem> genres;
	
	public UserBookManager() {
		books = new ArrayList<Book>();
		addNewBookActive = false;
		editBookActive = false;
		editingBook = null;
		genres = new ArrayList<SelectItem>();
		ArrayList<String> genreNames = null;
		try {
			 genreNames = StaticInfo.getGenre();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		for (int i=0; i<genreNames.size(); i++)
			genres.add(new SelectItem(genreNames.get(i)));
		
		Book book1 = new Book();
		book1.setParent(this);
		book1.setTitle("title1");
		book1.addAuthor("author1");
		book1.setCover("/images/dunya.png");
		Book book2 = new Book();
		book2.setParent(this);
		book2.setTitle("title2");
		book2.addAuthor("author2");
		book2.setCover("/images/dunya.png");
		Book book3 = new Book();
		book3.setParent(this);
		book3.setTitle("title3");
		book3.addAuthor("author3");
		book3.setCover("/images/dunya.png");
		Book book4 = new Book();
		book4.setParent(this);
		book4.setTitle("title4");
		book4.addAuthor("author4");
		book4.setCover("/images/dunya.png");
		books.add(book1);
		books.add(book2);
		books.add(book3);
		books.add(book4);
	}
	
	public List<SelectItem> getGenres() {
		return genres;
	}
	
	public List<Book> getBooks() {
		return books;
	}
	
	public void removeBook(Book book) {
		books.remove(book);
	}
	
	public void addNewBookActivate(ActionEvent event) {
		if (editingBook != null)
			editingBook = null;
		addingBook = new Book();
		setEditBookActive(false);
		setAddNewBookActive(true);
	}
	
	public void editBookActivate(Book book) {
		if (addingBook != null)
			addingBook = null;
		if (book != null)
			editingBook = book;
		setAddNewBookActive(false);
		setEditBookActive(true);
	}
	
	public void setAddNewBookActive(boolean addNewBookActive) {
		this.addNewBookActive = addNewBookActive;
	}

	public boolean getAddNewBookActive() {
		return this.addNewBookActive;
	}
	
	public void setEditBookActive(boolean editBookActive) {
		this.editBookActive = editBookActive;
	}
	
	public boolean getEditBookActive() {
		return this.editBookActive;
	}
	
	public boolean getManageBookFormActive() {
		return this.addNewBookActive || this.editBookActive;
	}
	
	public void setEditingBook(Book editingBook) {
		this.editingBook = editingBook;
	}
	
	public Book getEditingBook() {
		return this.editingBook;
	}
	
	public Book getAddingBook() {
		return addingBook;
	}
	
	public void setAddingBook(Book addingBook) {
		this.addingBook = addingBook;
	}
	
}
